package com.czd.support.controller;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.czd.base.model.Paginate;
import com.czd.support.common.Configuration;
import com.czd.support.model.FuJian;
import com.czd.support.service.FuJianService;

@Controller
@RequestMapping("/fujian")
public class FuJianController extends BaseController {
	
	
	@Autowired
	private FuJianService fuJianService;
	
	/**
	 * 删除指定记录
	 * @param id
	 * @param systemConfig
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/delete",method = RequestMethod.GET)
	public String delete(@PathVariable int id,FuJian fuJian,Model model){
		if (fuJianService.delete(fuJian)) {
			return "redirect:/fujian/list";
		} else {
			return error(model, "操作失败", "删除的信息内容已经不存在");
		}
	}
	
	
	/**
	 * 附件信息列表
	 */
	@RequestMapping(value = {"","/","list"},method = RequestMethod.GET)
    public String list(Model model,FuJian fuJian,@RequestParam(value = "pageNo",defaultValue = "1") int pageNo){
		fuJian.setPageNo(pageNo);
		Paginate p = fuJianService.queryForPaginate(fuJian);
//		System.out.println(p);
        model.addAttribute("paginate",p);
        return "fujian/list";
    }
	
	/**
	 * 录入附件
	 */
	@RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return "fujian/add";
    }
	
	@RequestMapping(value = "/add",method = RequestMethod.POST)
    public String save() {
       
        return "fujian/add";
    }
	
	/**
	 * 上传附件
	 * @throws IOException 
	 * @throws ParseException 
	 */
	@RequestMapping(value="/scfj" , method = RequestMethod.POST)
	public String save(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request) throws IOException, ParseException{
		String path = Configuration.getInstance().getAttach_url();
		String pathjq = path.substring(path.indexOf("/attach"));//path.substring(33, 46);
		String fileName = file.getOriginalFilename();
		String pathwj = pathjq + "/" +fileName;
		String releaseDate1 = request.getParameter("releaseDate");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date releaseDate=sdf.parse(releaseDate1); 
		String softType = request.getParameter("softType");
		int softtype = Integer.parseInt(softType);
		String description = request.getParameter("description");
		String namezdy = request.getParameter("namezdy");
		long size = file.getSize(); 
		//转换文件大小
		DecimalFormat df = new DecimalFormat("##.##");
		float fsize;String filesize;
		if (size < 1024 * 1024) {
			fsize = (float) ((float) size / (float) 1024);
			filesize = df.format(new Float(fsize).doubleValue()) + "KB";
		} else {
			fsize = (float) ((float) size / (float) (1024 * 1024));
			filesize = df.format(new Float(fsize).doubleValue()) + "MB";
		}
		FuJian f = new FuJian();
		f.setName(namezdy);f.setPath(pathwj);f.setDescription(description);f.setPriority(0);f.setCreateUser(0);
		f.setFileSize(filesize); f.setFileName(fileName);f.setType(softtype);f.setCreateTime(releaseDate);
		fuJianService.insert(f);
		
		File targetFile = new File(path, fileName);
		if(!targetFile.exists()){
            targetFile.mkdirs();  
        }
		
		try {
            file.transferTo(targetFile);  
        } catch (Exception e) {
            e.printStackTrace();  
        }
		
		return "redirect:/fujian/list"; 
	}
	
	/**
	 * 编辑内容
	 */
	@RequestMapping(value = "/{id}/edit",method = RequestMethod.GET)
    public String change(@PathVariable int id,Model model){
		FuJian fuJian = fuJianService.findById(id);
        if(fuJian != null){
            model.addAttribute("fuJian",fuJian);
            return "fujian/edit";
        }
        return error(model,"信息不存在","内容不存在");
    }
	
	@RequestMapping(value = "/{id}/edit", method = RequestMethod.POST)
	public String update(MultipartFile file,@PathVariable int id, FuJian fuJian,
			HttpServletRequest request, Model model) {
		//fuJian = fuJianService.findById(id);
		String fileName=file.getOriginalFilename();
		String path = Configuration.getInstance().getAttach_url();//文件上传路径
		String filePath=path.substring(path.indexOf("/attach"));
		if(fileName!=""){
			fuJian.setPath(filePath+"/"+fileName);
			long size = file.getSize(); 
			DecimalFormat df = new DecimalFormat("##.##");
			float fsize;String filesize;
			if (size < 1024 * 1024) {
				fsize = (float) ((float) size / (float) 1024);
				filesize = df.format(new Float(fsize).doubleValue()) + "KB";
			} else {
				fsize = (float) ((float) size / (float) (1024 * 1024));
				filesize = df.format(new Float(fsize).doubleValue()) + "MB";
			}
			fuJian.setFileSize(filesize);
			fuJian.setFileName(fileName);
			File targetFile = new File(path, fileName);
			if(!targetFile.exists()){
	            targetFile.mkdirs();  
	        }
			try {
	            file.transferTo(targetFile);  
	        } catch (Exception e) {
	            e.printStackTrace();  
	        }
		}
		String softType = request.getParameter("softType");
		int softtype = Integer.parseInt(softType);
		fuJian.setType(softtype);
		if (fuJianService.update(fuJian)) {
			return "redirect:/fujian/list";
		} else {
			return error(model, "操作失败", "信息内容已经不存在");
		}
	}

}
